import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { OrderService } from './order.service';
import { CreateOrderDto } from './dto/create-order.dto';
import { QueryOrderDto } from './dto/query-order.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('订单管理')
@ApiBearerAuth()
@Controller('order')
@RequireLogin()
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @Post()
  @ApiOperation({ summary: '创建订单' })
  @ApiResponse({ status: 201, description: '创建成功' })
  async create(
    @Body() createOrderDto: CreateOrderDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.orderService.create(createOrderDto, userId);
  }

  @Get()
  @ApiOperation({ summary: '查询订单列表' })
  async findAll(
    @Query() queryOrderDto: QueryOrderDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.orderService.findAll(queryOrderDto, userId);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取订单详情' })
  @ApiParam({ name: 'id', description: '订单ID' })
  async findOne(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.orderService.findOne(+id, userId);
  }

  @Post(':id/pay')
  @ApiOperation({ summary: '支付订单' })
  @ApiParam({ name: 'id', description: '订单ID' })
  @ApiResponse({ status: 200, description: '支付成功' })
  async pay(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.orderService.pay(+id, userId);
  }

  @Post(':id/ship')
  @ApiOperation({ summary: '发货' })
  @ApiParam({ name: 'id', description: '订单ID' })
  @ApiResponse({ status: 200, description: '发货成功' })
  async ship(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.orderService.ship(+id, userId);
  }

  @Post(':id/receive')
  @ApiOperation({ summary: '确认收货' })
  @ApiParam({ name: 'id', description: '订单ID' })
  @ApiResponse({ status: 200, description: '确认收货成功' })
  async receive(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.orderService.receive(+id, userId);
  }

  @Post(':id/cancel')
  @ApiOperation({ summary: '取消订单' })
  @ApiParam({ name: 'id', description: '订单ID' })
  @ApiResponse({ status: 200, description: '取消成功' })
  async cancel(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.orderService.cancel(+id, userId);
  }
}
